Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

416
Views
Agregar botones de opción con validación en Mi cuenta > editar cuenta en WooCommerce

A través del siguiente código, he agregado botones de opción en la página de perfil de Mi cuenta en WooCommerce.

El problema es que no puedo hacer que el botón de opción sea obligatorio y no puedo mostrar un aviso cuando no se hace ninguna elección.

Aquí está mi código...

HTML

 // Add & display radio <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide"> <div class="myAccount_radiobtn"> <label for="gender"><?php esc_html_e( 'Gender', '' ); ?>&nbsp;<span class="required">*</span></label> <div class="my_account_profile_radio"> <input type="radio" required name="gender" id="gender value="Male" <?php if('Male'==esc_attr(get_the_author_meta('gender',$user->ID ))) echo 'checked="checked"'; ?> class="radio" />&nbsp;&nbsp;Male<br /> <input type="radio" required name="gender" id="gender value="Female" <?php if('Female'==esc_attr(get_the_author_meta('gender',$user->ID ))) echo 'checked="checked"'; ?> class="radio" />&nbsp;&nbsp;Female<br /> <input type="radio" required name="gender" id="gender value="Others" <?php if ('Others'==esc_attr(get_the_author_meta('gender',$user->ID ))) echo 'checked="checked"'; ?> class="radio" />&nbsp;&nbsp;Others </div> </div> </p>

PHP

 // Save radio function save_user_account_details( $user_id ) { if( isset( $_POST['gender'] ) ) update_user_meta( $user_id, 'gender', sanitize_text_field( $_POST['gender'] ) ); } add_action( 'woocommerce_save_account_details', 'save_user_account_details', 12, 1 ); // Validation radio function myaccount_fields_validation2 ( $errors ) { if ( isset( $_POST['gender'] ) ) { if(strlen($_POST['gender'])<4 ) $errors->add( 'error', __( '<strong>Gender</strong> is a required field.', '' ),''); } } add_action( 'user_profile_update_errors','myaccount_fields_validation2', 10, 1 );

¿Algún consejo?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Su código contiene algunos errores/errores

  • La etiqueta <div> NO puede estar dentro de la etiqueta <p> , porque el párrafo se dividirá en el punto donde se ingresa la etiqueta <div> .
  • La configuración requerida para todas las entradas es más clara, pero no necesaria (a menos que se generen dinámicamente botones de opción).
  • Tiene un error tipográfico en la ID , no se cerrará correctamente debido a la falta de un "
  • El user_profile_update_errors no se aplica aquí, use woocommerce_save_account_details_errors en su lugar
  • Aunque se puede agregar el código para crear los botones de radio, al modificar el archivo de plantilla lo agregué en mi respuesta a través de un gancho para mayor claridad.

Entonces obtienes:

 // Add field function action_woocommerce_edit_account_form_start() { // Retrieve the current user object. $user = wp_get_current_user(); ?> <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide"> <label for="gender"><?php esc_html_e( 'Gender', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label> <input type="radio" class="radio" name="gender" value="Male" <?php if( 'Male' == esc_attr( get_the_author_meta( 'gender', $user->ID ) ) ) echo 'checked'; ?>>&nbsp;&nbsp;Male<br /> <input type="radio" class="radio" name="gender" value="Female" <?php if( 'Female' == esc_attr( get_the_author_meta( 'gender', $user->ID ) ) ) echo 'checked'; ?>>&nbsp;&nbsp;Female<br /> <input type="radio" class="radio" name="gender" value="Others" <?php if ( 'Others' == esc_attr( get_the_author_meta( 'gender', $user->ID ) ) ) echo 'checked'; ?>>&nbsp;&nbsp;Others </p> <?php } add_action( 'woocommerce_edit_account_form_start', 'action_woocommerce_edit_account_form_start' ); // Validate - my account function action_woocommerce_save_account_details_errors( $args ) { if ( ! isset( $_POST['gender'] ) ) { $args->add( 'error', '<strong>' . __( 'Gender', 'woocommerce' ) . '</strong> ' . __( 'is a required field.', 'woocommerce' ) ); } } add_action( 'woocommerce_save_account_details_errors', 'action_woocommerce_save_account_details_errors', 10, 1 ); // Save - my account function action_woocommerce_save_account_details( $user_id ) { if ( isset( $_POST['gender'] ) ) { // Update field update_user_meta( $user_id, 'gender', sanitize_text_field( $_POST['gender'] ) ); } } add_action( 'woocommerce_save_account_details', 'action_woocommerce_save_account_details', 10, 1 );
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!